from symbide import ModuleManager, LogManager

def _clear(namespace, *args):
    '''
    Deletes all given arguments out of the local namespace.
    If called with no arguments, the dictionary is emptied completely.
    '''
    
    if args:
        for key, item in namespace.items():
            if item in args:
                del namespace[key]
    else:
        namespace.clear()

def _show(objects, plot=None):
    xml=None
    if plot is None:
        #this is the case when using matplotlib
        if ModuleManager.hasModule('pylab'):
            from symbide.src.extensions.matplotlib.additionalFunctions import matplot_toxml
            xml = matplot_toxml()
        else:
            #TODO: raise a meaningful error when show is called without arguments and pylab is not installed
            LogManager.alert('"show" invoken without arguments. This makes only sense for matplotlib plots, but pylab is not installed.')
    elif isinstance(plot, str):
        xml = '<img src="%s"/>'%plot
    elif ModuleManager.hasModule('sympy'):
        from sympy import Basic, printing
        if isinstance(plot, Basic):
            xml = '<latex>%s</latex>'%printing.latex(plot)
    else:
        try:
            xml = plot.toxml()
        except:
            #TODO: raise a meaningful error
            LogManager.alert('"show" can\'t handle this argument:%s'%str(plot)) 
        
    if xml:
        objects.append(xml)
        print '__objects__[%s]\n'%str(len(objects)-1)